home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP1292.ARJ / WC.C < prev    next >
C/C++ Source or Header  |  1992-07-03  |  1KB  |  66 lines

  1. /*
  2.    File wc.c - a sample word count program
  3.    Written and submitted to public domain by Jay Elkes
  4.    April, 1992
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. int main (int argc, char *argv[])
  11. {
  12.       FILE *infileptr;
  13.       char infile[80];
  14.  
  15.       long int nl = 0;
  16.       long int nc = 0;
  17.       long int nw = 0;
  18.  
  19.       int state = 0;
  20.       const int  NEWLINE = '\n';
  21.       int  c;
  22.  
  23. /*  The program name itself is the first command line arguement so we
  24.     ignore it (argv[0]) when showing user entered parameters. */
  25.  
  26.       switch (argc - 1)
  27.       {
  28.       case (0):
  29.             printf("no parameters\n");
  30.             return 12;
  31.       case (1):
  32.             break;
  33.       default:
  34.             printf("too many parameters\n");
  35.             return 12;
  36.       }
  37.  
  38.       strcpy(infile,argv[1]);
  39.  
  40.       infileptr = fopen(infile,"rb");
  41.       if (infileptr == NULL)
  42.       {
  43.             printf("Cannot open %s\n",infile);
  44.             return 12;
  45.       }
  46.  
  47.       while ((c = getc(infileptr)) != EOF)
  48.       {
  49.             ++nc;
  50.             if (c == NEWLINE)
  51.                   ++nl;
  52.             if (isspace(c))
  53.                   state = 0;
  54.             else if (state == 0)
  55.             {
  56.                   state = 1;
  57.                   ++nw;
  58.             }
  59.       }
  60.  
  61.       /* Final Housekeeping */
  62.  
  63.       printf("%ld Lines, %ld Words, %ld Characters", nl, nw, nc);
  64.       return 0;
  65. }
  66.